// 通过url地址下载文件功能
//file 是文件的URL
//name 下载文件名
export function downloadFile(url) {
  fetch(url)
    .then((res) => res.blob())
    .then(() => {
      const a = document.createElement("a");
      a.href = url;
      document.body.appendChild(a);
      a.click();
      document.body.removeChild(a);
    })
    .catch((err) => {
      console.log("下载报错");
    });
}

//post 数据请求
export function downloadPost(url,body) {
  fetch(url, {
    method: "POST",
    headers:{
      'Content-Type':'application/x-www-form-urlencoded'
    },
    body: body,
  }).then((response) => {
    if (response.ok) {
      return response.json();
    } else {
      return Promise.reject({
        status: response.status,
        statusText: response.statusText,
      });
    }
  });
}
